package com.event.controller;


import com.alibaba.fastjson.JSON;
import com.event.pojo.QaQuestion;
import com.event.service.QaQuestionService;
import com.event.util.GetIp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 问题列表 前端控制器
 * </p>
 *
 * @author event
 * @since 2021-10-06
 */
@RestController
@RequestMapping("/question")
@CrossOrigin(origins = "*",maxAge = 3600)
@Api(value = "问答",tags = "问答")
public class QaQuestionController {
    @Autowired
    QaQuestionService questionService;

    @GetMapping("/selectQuestion")
    @ApiOperation(value = "获取所有已通过审核的问答的接口,如果传入为空则为查询全部",notes = "传值进来")
    public JSON selectQuestion(QaQuestion qa){
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        String msg;
        List<QaQuestion> qlist = questionService.selectQuestion(qa);
        System.out.println(qlist);
        if(qlist.size()>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        map.put("count",qlist.size());
        map.put("data",qlist);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }

    @PostMapping("/insertQuestion")
    @ApiOperation(value = "添加问答接口",notes = "传值进来")
    public JSON insertQuestion(QaQuestion qa, HttpServletRequest request){
        GetIp getIp = new GetIp(request);

        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        String msg;
        int i = questionService.insertQuestion(qa);
        if(i>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @PostMapping("/updateQuestion")
    @ApiOperation(value = "更新问答审核状态",notes = "一定要传入问题id")
    public JSON updateQuestion(QaQuestion qa){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        int i = questionService.updateQuestion(qa);
        if(i>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }

    @PostMapping("/deleteQuestion")
    @ApiOperation(value = "更新问答审核状态",notes = "只传入问题id")
    public JSON deleteQuestion(QaQuestion qa){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        int i = questionService.deleteQuestion(qa);
        if(i>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
}

